import time
import datetime
from webbrowser import Chrome

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
# as EC 取别名
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

# #编写自动化脚本，具体步骤
# #1.打开浏览器----驱动管理
# ChromeIns=ChromeDriverManager().install()
# ###创建谷歌浏览器驱动对象
# driver=webdriver.Chrome(service=Service(ChromeIns))
# time.sleep(2)
# #2.输入百度网址：https://www.baidu.com（网址一定要完整，不要写成www.baidu.com)
# driver.get("https://www.baidu.com")
# time.sleep(2)
# #3.找到百度输入框，输入关键词“迪丽热巴”
# driver.find_element(By.CSS_SELECTOR, "#chat-textarea").send_keys("迪丽热巴")
# time.sleep(2)
# #点击一下其他地方让，让驱动定位到百度一下这个按钮
# driver.find_element(By.CSS_SELECTOR,'#wrapper').click()
# #4.点击“百度一下”按钮
# driver.find_element(By.CSS_SELECTOR, "#chat-submit-button").click()
# time.sleep(2)
# #等待搜索结果加载
# time.sleep(3)
# # #5.关闭浏览器
# driver.quit()

#send.keys(...)接口的使用 clear() click()
# # 1.打开浏览器驱动管理
# ChromeIns = ChromeDriverManager().install()
# #创建浏览器对象
# driver=webdriver.Chrome(service=Service(ChromeIns))
# #2.输入百度网址
# driver.get("https://www.baidu.com")
# time.sleep(2)
# #3.向输入框中输入bilbil
# driver.find_element(By.CSS_SELECTOR, "#chat-textarea").send_keys("bilbil")
# time.sleep(2)
# #清除输入框中的内容
# driver.find_element(By.CSS_SELECTOR, "#chat-textarea").clear()
# #向输入框中输入wegame
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR, "#chat-textarea").send_keys("wegame")
# #点击别的地方让，驱动可以定位到百度一下
# # driver.find_element(By.CSS_SELECTOR, "#wrapper_wrapper").click()
# # time.sleep(2)
# #4.点击百度一下
# driver.find_element(By.XPATH, '//*[@id="chat-submit-button"]').click()
# #等待一会查看结果
# time.sleep(3)
#
# driver.quit()

# 1.打开浏览器驱动管理
# ChromeIns = ChromeDriverManager().install()
# #创建浏览器对象
# driver=webdriver.Chrome(service=Service(ChromeIns))
# #2.输入百度网址
# driver.get("https://www.baidu.com")
# time.sleep(2)
# #3.向输入框中输入bilbil
# driver.find_element(By.CSS_SELECTOR, "#chat-textarea").send_keys("bilbil")
# time.sleep(2)
# #清除输入框中的内容
# driver.find_element(By.CSS_SELECTOR, "#chat-textarea").clear()
# #向输入框中输入wegame
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR, "#chat-textarea").send_keys("wegame")
# #点击别的地方让，驱动可以定位到百度一下
# # driver.find_element(By.CSS_SELECTOR, "#wrapper_wrapper").click()
# # time.sleep(2)
# #4.点击百度一下
# driver.find_element(By.XPATH, '//*[@id="chat-submit-button"]').click()
# #等待一会查看结果
# time.sleep(3)
#
# driver.quit()

#test 获取文本的使用
# ChromeIns=ChromeDriverManager().install()
# driver=webdriver.Chrome(service=Service(ChromeIns))
# driver.get("https://www.baidu.com")
# #获取文本
# text=driver.find_element(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li:nth-child(1) > a > span.title-content-title").text
# print(text)
# #assert test == "树立全球妇女事业发展新的里程碑"
# #将获取的文本打印出来
# driver.quit()

#元素属性值不等于文本信息
# ChromeIns=ChromeDriverManager().install()
# driver=webdriver.Chrome(service=Service(ChromeIns))
# driver.get("https://www.baidu.com")
# text=driver.find_element(By.CSS_SELECTOR, "#chat-textarea").text
# assert text == "百度一下"
# driver.quit()

# #正确获取百度一下的方式
# ChromeIns=ChromeDriverManager().install()
# driver=webdriver.Chrome(service=Service(ChromeIns))
# driver.get("https://www.baidu.com")
# text=driver.find_element(By.CSS_SELECTOR, "#su").get_attribute("value")
# print("text: "+text)
# # assert text == "百度一下"
# #获取当前页面的url
# url=driver.current_url
# print("url: "+url)
# #获取当前页面标题
# title=driver.title
# print("title: "+title)
# driver.quit()

#跳转功能
# ChromeIns=ChromeDriverManager().install()
# driver=webdriver.Chrome(service=Service(ChromeIns))
# driver.get("https://www.baidu.com")
# print("before"+driver.current_url)
# print("before"+driver.title)
# driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(6)").click()
# time.sleep(3)
# print("after"+driver.current_url)
# print("after"+driver.title)
# driver.quit()

#虽然打开了新界面，但是url和title还是之前的
#窗口
# ChromeIns=ChromeDriverManager().install()
# driver=webdriver.Chrome(service=Service(ChromeIns))
# driver.get("http://www.baidu.com")
# driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(6)").click()
# #获取当前页面的句柄
# time.sleep(2)
# print("before url: " + driver.current_url)
# print("before title: " + driver.title)
# Curhandle=driver.current_window_handle
# #获取所有页面的句柄
# AllHandles=driver.window_handles
# #遍历所有页面，切换到新的页面
# for handle in AllHandles:
#     if Curhandle != handle:
#         driver.switch_to.window(handle)
# print("after url: " + driver.current_url)
# print("after title: " + driver.title)
# driver.quit()

# #窗口大小设置
# ChromeIns=ChromeDriverManager().install()
# driver = webdriver.Chrome(service=Service(ChromeIns))
# driver.get("http://baidu.com")
# #手动设置窗口大小
# # driver.set_window_size(1024, 768)
# #窗口全屏化
# # driver.fullscreen_window()
# #窗口最大化
# # driver.maximize_window()
# time.sleep(4)
# #窗口最小化
# # driver.minimize_window()
# driver.quit()

# #屏幕截图
# ChromeIns=ChromeDriverManager().install()
# driver = webdriver.Chrome(service=Service(ChromeIns))
# driver.get("http://baidu.com")
# #添加屏幕截图
# # filename="./test.png"
# # filename="./images/test.png"
# filename="autotest-" + datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S") + ".png"
# driver.save_screenshot("./images/" + filename)
# #退出整个浏览器
# driver.quit()
# #退出该窗口
# # driver.close()

#弹窗
# ChromeIns=ChromeDriverManager().install()
# driver = webdriver.Chrome(service=Service(ChromeIns))
# driver.get("http://baidu.com")
# #切换到弹窗
# alert=driver.switch_to.alert
# #确认
# alert.accept()
# #取消
# alert.dismiss()

# #等待
# ChromeIns=ChromeDriverManager().install()
# driver = webdriver.Chrome(service=Service(ChromeIns))
# driver.get("http://baidu.com")
# #强制等待
# time.sleep(3)
# #隐式等待 在设置时间内一直加载元素，知道超时报错
# # implicitly_wait(3)
# #显示等待 针对某一条指令生效

# #浏览器导航
# ChromeIns=ChromeDriverManager().install()
# driver = webdriver.Chrome(service=Service(ChromeIns))
# #打开网站
# driver.get("http://baidu.com")
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR, "#chat-textarea").send_keys("bilbil")
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR, "#chat-submit-button").click()
# time.sleep(2)
# #浏览器的前进后退刷新
# #浏览器的后退
# driver.back()
# time.sleep(2)
# #浏览器的前进
# driver.forward()
# time.sleep(2)
# #浏览器的刷新
# driver.refresh()
# time.sleep(2)
# driver.quit()

#文件上传
















